package com.bc.web.controller.admin;

import java.io.File;

import com.bc.web.config.R;
import com.bc.web.model.Org;
import com.bc.web.model.OrgUser;
import com.fs.common.extend.FSController;
import com.fs.common.util.FSDate;
import com.fs.common.util.FSIDGenerate;
import com.fs.common.util.FSJedis;
import com.jfinal.aop.Before;
import com.jfinal.kit.PathKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

public class OrgController extends FSController{
	
	public void search(){
		Page<Org> items = Org.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("org/orgList.html");
	}
	
	public void lookup(){
		Page<Org> items = Org.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("org/orgLookup.html");
	}
	
	public void addPage(){
		render("org/orgAdd.html");
	}
	
	public void updatePage(){
		Org org = Org.dao.findById(getPara("id"));
		setAttr("org", org);
		render("org/orgUpdate.html");
	}
	
	@Before(Tx.class)
	public void add(){
		Org org = getModel(Org.class,true);
		org.setCode(FSIDGenerate.getNextOrgCode());
		org.setSafeKey(FSIDGenerate.getUUID().toUpperCase());
		org.setCreateTime(FSDate.getDateTime());
		org.setUpdateTime(FSDate.getDateTime());
		org.save();
		if (R.IS_REDIS) {
			FSJedis.update(R.RK_ORG, org.getCode(), org.getSafeKey());
		}
		renderSuccess();
	}
	
	public void update(){
		Org org = getModel(Org.class);
		org.setUpdateTime(FSDate.getDateTime());
		org.update();
		if (R.IS_REDIS) {
			FSJedis.update(R.RK_ORG, org.getCode(), org.getSafeKey());
		}
		renderSuccess();
	}
	
	public void view(){
		Org org = Org.dao.findById(getPara("id"));
		setAttr("org", org);
		render("org/orgView.html");
	}
	
	public void delete(){
		Org org = Org.dao.findById(getPara("id"));
		org.delete();
		if (R.IS_REDIS) {
			FSJedis.remove(R.RK_ORG, org.getCode());
		}
		renderSuccess(false);
	}
	
	
	private static final String REPORT_PATH = PathKit.getWebRootPath()+File.separator +"payimg"+File.separator; //SystemEnum.FilePath.ReportTmp.getValue();
	@SuppressWarnings("unused")
	public void downloadMyzip() {
		String orgCode = getPara("code");
		//System.out.println("参数信息:" + orgCode + ",路径:" + (REPORT_PATH + orgCode + ".zip"));
		File file = new File((REPORT_PATH + orgCode + ".zip"));
		if(file == null || !file.exists()) {
			renderFail("合同文件不存在,请联系管理员上传合同!", false);
		}else {
			renderFile(file);
		}
	}
	
}
